#import "MainWindowController+NavigationBar.h"
#import "RNCategory.h"
#import "RNCatalog.h"

@implementation MainWindowController (MainWindowControllerNavigationBarMethods)

+(NSSet *)keyPathsForValuesAffectingRootObjectHeirarchyString {
    return [NSSet setWithObject:@"rootObject"];
}

-(NSString *)rootObjectHeirarchyString {
    NSMutableString *string = [NSMutableString stringWithFormat:@"/%@", [self rootObject] == nil ? @"" : [[self rootObject] title]];
    id parent = [[self rootObject] parent];
    while (parent) {
        [string insertString:[NSString stringWithFormat:@"/%@", [parent title]] atIndex:0];
        parent = [parent parent];
    }
    if ([[self rootObject] isKindOfClass:[RNCategory class]])
        [string insertString:@"/Catalog" atIndex:0];
    return string;
}

-(IBAction)navigationBarPathControlClicked:(id)sender {
    NSPathComponentCell *clickedCell = [sender clickedPathComponentCell];
    if (!clickedCell)
        return;
    NSArray *pathCells = [sender pathComponentCells];
    if (!pathCells || [pathCells count] == 0)
        return;
    NSUInteger clickedCellIndex = [pathCells indexOfObject:clickedCell];
    if (clickedCellIndex == NSNotFound)
        return;
    id currentParent = [self rootObject];
    for (NSUInteger pathCellIndex = [pathCells count] - 1; pathCellIndex != clickedCellIndex; pathCellIndex--) {
        currentParent = [currentParent parent];
        if (!currentParent && [[self rootObject] isKindOfClass:[RNCategory class]])
            currentParent = [RNCatalog sharedCatalog];
    }
    [self selectRootObject:currentParent];
}

+(NSSet *)keyPathsForValuesAffectingOnlyOnePathCell {
    return [NSSet setWithObject:@"rootObject"];
}

-(BOOL)onlyOnePathCell {
    return [[[self pathControl] pathComponentCells] count] == 1;
}

@end
